package com.william.jdbcplus.core.dialect.ddl.table;

import com.william.jdbcplus.common.enums.DbType;
import com.william.jdbcplus.core.dialect.ddl.table.impl.MysqlTable;
import com.william.jdbcplus.core.dialect.ddl.table.impl.OracleTable;
import com.william.jdbcplus.core.dialect.ddl.table.impl.SqlServerTable;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 表工厂
 *
 * @author william
 */
public class TableFactory {
    private static final Map<DbType, ITable> FACTORY_CACHE = new ConcurrentHashMap<>();

    static {
        for (DbType dbType : DbType.values()) {
            FACTORY_CACHE.put(dbType, getByDbType(dbType));
        }
    }

    /**
     * 获取实例
     *
     * @param dbType
     * @return
     */
    public static ITable getInstance(DbType dbType) {
        ITable iTable = FACTORY_CACHE.get(dbType);
        return iTable;
    }

    /**
     * 根据数据库类型选择不同建表方言
     *
     * @param dbType 数据库类型
     */
    private static ITable getByDbType(DbType dbType) {
        switch (dbType) {
            case MYSQL:
                return new MysqlTable();
            case ORACLE:
                return new OracleTable();
            case SQL_SERVER:
                return new SqlServerTable();
            case SQL_SERVER2005:
                return new SqlServerTable();
            default:
                return new OracleTable();
        }
    }
}
